<template>
  <el-scrollbar style="height:100%;">
    <div class="container">
      <el-container>
        <el-main :class="personInfoClass">
          <div class="myCard">
            <div class="myCard-photo">
              <div class="wood"></div>
            </div>
            <div class="myCard-intro">
              <p class="title"><i class="iconfont icon-mingpian"></i>我的名片</p>
              <ul class="details">
                <li>网络ID : Kpods</li>
                <li>职业 : 学生</li>
                <li>现居 : 广东省湛江市</li>
                <li>E-mail : kpods_ovo@163.com</li>
              </ul>
            </div>
          </div>
          <el-card class="box-card" v-for="(item,index) in articles" :key="index">
            <div class="img" :style="{backgroundImage: `url(${item.img})`}" @click="enterBlog(item._id)"></div>
            <div class="article" @click="enterBlog(item._id)">
              <ul class="title">
                <li>{{item.title}}</li>
                <li :class="{
                'cate_li_life':item.category=='生活琐事',
                'cate_li_study':item.category=='学习记录',
                'cate_li_feel':item.category=='有感而发',
                }">{{item.category}}</li>
                <li><i class="iconfont icon-shijian"></i>{{item.add_time}}</li>
                <li><i class="iconfont icon-yuedu"></i>{{item.visits}}</li>
                <li><i class="iconfont icon-dianzan"></i>{{item.dianzan}}</li>
              </ul>
              <div class="article-content" v-html="item.intro_content"></div>
            </div>
          </el-card>
          <!-- 回到顶部小火箭 -->
          <template>
            <el-backtop>
              <div class="huojian">
                <i class="iconfont icon-huojian"></i>
              </div>
            </el-backtop>
          </template>
          <transition name="fade">
            <loading v-if="isLoading"></loading>
          </transition>
        </el-main>
        <el-footer>
          <div class="text1">Copyright © {{year}} by Kpods. All Rights Reserved.</div>
          <div class="text2">备案号......</div>
        </el-footer>
      </el-container>
    </div>
  </el-scrollbar>
</template>

<script>
import Loading from './loading/loading.vue'
import marked from 'marked'
export default {
  components: { Loading },
  data() {
    return {
      value: new Date(),
      articles: [],
      year: new Date().getFullYear(),
      inputContent: '',
      // Loading状态
      isLoading: true,
      // 窗口宽度
      windowWidth: document.documentElement.clientWidth
    }
  },
  mounted() {
    this.getAllArticles()
  },
  // 解析md数据
  computed: {
    compiledMarkdown() {
      this.articles.forEach((item, index) => {
        item.intro_content[index] = marked(item.intro_content || '', { sanitize: true })
      })
      return this.articles
    },
    // 判断屏幕宽度
    personInfoClass() {
      if (this.windowWidth >= 1080) {
        return 'PC_layout'
      } else if (this.windowWidth < 1080 && this.windowWidth >= 812) {
        return 'middle_layout'
      } else {
        return 'mobile_layout'
      }
    }
  },
  methods: {
    // 获取所有文章信息
    getAllArticles() {
      this.isLoading = true
      this.$http.get('article', { params: { inputContent: this.inputContent } }).then(response => {
        this.articles = response.data
        this.articles.reverse()
        this.isLoading = false
      })
    },
    // 进入一篇博客
    enterBlog(id) {
      this.$router.push(`/Blog/article/${id}`)
      this.$http.put(`article/${id}`).then(response => {
      })
    }
  },
  watch: {
    '$store.state.inputContent'(val, oldVal) {
      this.inputContent = val
      this.getAllArticles()
    },
    // 监听屏幕宽度
    '$store.state.screenWidth': function(val) { // 监听屏幕宽度变化
      this.windowWidth = val
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-scrollbar__wrap {
  overflow-x: hidden;
}
/deep/.el-scrollbar__view {
  height: 100%;
}
.container {
  height: 100%;
}
.el-container {
  min-height: 100%;
}
.el-main {
  padding: 35px 250px 68px;
  margin-top: 60px;
  // padding-top: 60px;
  height: 100%;
  background-image: linear-gradient(
    to right,
    rgba(254, 172, 94, 0.6),
    rgba(199, 121, 208, 0.6)
  );
  // background-color: #b3badd;
  .myCard {
    float: left;
    margin-left: -87px;
    margin-right: 20px;
    .myCard-photo {
      width: 250px;
      height: 250px;
      background-color: #110f0e;
      border: 5px solid #fff;
      border-radius: 5px;
      background-image: url("../assets/portrait.png");
      background-size: 100%, 100%;
      animation: appear 1s cubic-bezier(0.1, 0.7, 0.21, 0.96);
      .wood {
        width: 20px;
        height: 50px;
        background-color: #fff;
        margin: 0 auto;
        margin-top: -50px;
      }
      @keyframes appear {
        from {
          transform: translateY(-295px);
        }
        to {
          transform: translateY(0px);
        }
      }
    }
    .myCard-intro {
      position: relative;
      width: 240px;
      height: 220px;
      background-color: #fff;
      padding: 10px;
      margin-top: 30px;
      box-shadow: 0 0 10px rgb(177, 177, 177);
      animation: opacityAppear 1s ease;
      .title {
        display: inline-block;
        width: 90%;
        padding: 10px 0px;
        font-size: 16px;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        color: #333;
        font-weight: bold;
        border-bottom: 1px solid #999;
        i {
          color: rgb(255, 162, 0);
          margin-right: 10px;
        }
      }
      .details {
        position: absolute;
        height: 140px;
        top: 15%;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        color: #333;
        font-size: 15px;
        margin-top: 40px;
        padding: 0 15px;
      }
      @keyframes opacityAppear {
        from {
          opacity: 0;
        }
        to {
          opacity: 1;
        }
      }
    }
  }
  .box-card {
    margin: 0px 0px 20px 193px;
    background-color: rgba(255, 255, 255, 0.75);
    cursor: pointer;
    .img {
      float: left;
      width: 100px;
      height: 100px;
      background-color: #fff;
      margin-bottom: 20px;
      background-size: 100% 100%;
    }
    .article {
      margin-left: 130px;
      .title {
        margin-bottom: 20px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        display: flex;
        li {
          align-self: center;
          margin-right: 20px;
          font-size: 14px;
          color: #737373;
          i {
            margin-right: 5px;
          }
        }
        li:first-child {
          width: 180px;
          font-size: 20px;
          font-weight: bold;
          color: #333;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        li:nth-child(2) {
          width: 200px;
          font-size: 16px;
          padding-left: 10px;
          // margin-right: 200px;
          // border-left: 5px solid #ff9e5f;
          // color: #ff9e5f;
        }
        .cate_li_life {
          border-left: 5px solid #8ece6a;
          color: #8ece6a;
        }
        .cate_li_study {
          border-left: 5px solid #ff9e5f;
          color: #ff9e5f;
        }
        .cate_li_feel {
          border-left: 5px solid #6c93eb;
          color: #6c93eb;
        }
      }
      .article-content {
        display: -webkit-box;
        overflow: hidden;
        text-overflow: ellipsis;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        line-height: 26px;
      }
    }
  }
}
.middle_layout {
  padding: 35px 100px 68px;
}
.mobile_layout {
  padding: 35px 30px 68px;
  .myCard {
    display: none;
  }
  .box-card {
    margin: 0px 0px 20px 0px;
    background-color: rgba(255, 255, 255, 0.75);
    cursor: pointer;
    .img {
      float: left;
      width: 70px; // 修改100->70
      height: 70px; // 修改100->70
      background-color: #fff;
      margin-bottom: 20px;
      background-size: 100% 100%;
    }
    .article {
      margin-left: 80px; // 修改130->80
      .title {
        margin-bottom: 12px; // 修改20->12
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        display: flex;
        li {
          align-self: center;
          margin-right: 20px;
          font-size: 12px; // 修改14->12
          color: #737373;
          i {
            margin-right: 5px;
          }
        }
        li:first-child {
          width: 180px;
          font-size: 16px; // 修改20->16
          font-weight: bold;
          color: #333;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        li:nth-child(2) {
          width: 200px;
          font-size: 16px;
          padding-left: 10px;
          // margin-right: 200px;
          // border-left: 5px solid #ff9e5f;
          // color: #ff9e5f;
        }
        .cate_li_life {
          border-left: 5px solid #8ece6a;
          color: #8ece6a;
        }
        .cate_li_study {
          border-left: 5px solid #ff9e5f;
          color: #ff9e5f;
        }
        .cate_li_feel {
          border-left: 5px solid #6c93eb;
          color: #6c93eb;
        }
      }
      .article-content {
        display: -webkit-box;
        overflow: hidden;
        text-overflow: ellipsis;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        line-height: 18px; // 修改26->18
        font-size: 12px; // 增加的样式
      }
    }
  }
}
.el-footer {
  // padding: 10px 250px;
  background-color: #24283b;
  color: #fff;
  border-top: 1px solid #cccccc;
  font-size: 14px;
  font-weight: 200;
  text-align: center;
  .text1 {
    padding-top: 10px;
  }
  .text2 {
    padding-bottom: 10px;
  }
}
.choose {
  background-color: #fff;
  color: rgb(0, 162, 255);
  border-radius: 3px;
}
</style>
